package com.sundablog.controller.backend.system.upms.role;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.deser.Deserializers.Base;
import com.sundablog.pojo.AdminRole;
import com.sundablog.pojo.AdminRolePermission;
import com.sundablog.result.BaseResult;
import com.sundablog.result.LayUiResult;
import com.sundablog.service.backend.system.upms.role.RoleService;

import cn.hutool.setting.Setting;

/**
 * 角色权限控制器
 * @ClassName:  RoleController   
 * @Description:角色权限控制器   
 * @author: 哒哒 
 * @date:   2018年3月14日 下午7:10:26   
 *     
 * @Copyright: 2018 www.sundablog.com Inc. All rights reserved.
 */
@Controller
@RequestMapping("/backend/system/upms/role")
public class RoleController {
	
	@Autowired
	private RoleService roleService;

	/**
	 * 角色界面显示
	 * @Title: index   
	 * @Description: 添加界面显示  
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	@RequiresPermissions("backend_system_upms_role_index")
	@RequestMapping("/index")
	public String index() {
		return "backend/system/upms/role/index";
	}
	
	/**
	 * 添加角色界面显示
	 * @Title: addIndex   
	 * @Description: 添加角色界面显示
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	@RequiresPermissions("backend_system_upms_role_add")
	@RequestMapping("/add")
	public String addIndex() {
		return "backend/system/upms/role/add";
	}
	
	/**
	 * 设置权限界面显示
	 * @Title: settingIndex   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	@RequiresPermissions("backend_system_upms_role_setting")
	@RequestMapping("/setting")
	public String settingIndex() {
		return "backend/system/upms/role/setting";
	}
	
	
	/**
	 * 删除角色
	 * @Title: delectRole   
	 * @Description:  删除角色
	 * @param: @param roleId
	 * @param: @return      
	 * @return: BaseResult      
	 * @throws
	 */
	@RequiresPermissions("backend_system_upms_role_delectRole")
	@RequestMapping("/delectRole")
	@ResponseBody
	public BaseResult delectRole(int roleId) {
		int i = roleService.delectRoleByRoleId(roleId);
		if (i > 0) {
			return BaseResult.build(200, BaseResult.delectSuccess);
		} else {
			return BaseResult.build(201, BaseResult.delectFail);
		}
	}
	
	
	/**
	 * 分页查询
	 * @Title: findPagingQuery   
	 * @Description: 分页查询   
	 * @param: @param page
	 * @param: @param limit
	 * @param: @return      
	 * @return: LayUiResult      
	 * @throws
	 */
	@RequestMapping("/findPagingQuery")
	@ResponseBody
	public LayUiResult findPagingQuery(@RequestParam(value="page", defaultValue="1") int page,@RequestParam(value="limit", defaultValue="10") int limit) {
		LayUiResult result = roleService.findPagingQuery(page, limit);
		return result;
	}
	
	/**
	 * 查询全部角色
	 * @Title: selectRole   
	 * @Description:查询全部角色
	 * @param: @return      
	 * @return: BaseResult      
	 * @throws
	 */
	@RequestMapping("/selectRole")
	@ResponseBody
	public BaseResult selectRole() {
		List<AdminRole> list = roleService.selectRole();
		return BaseResult.build(200, "", list);
	}
	
	/**
	 * 添加角色
	 * @Title: addRole   
	 * @Description: 添加角色
	 * @param: @param adminRole
	 * @param: @return      
	 * @return: BaseResult      
	 * @throws
	 */
	@RequestMapping("/addRole")
	@ResponseBody
	public BaseResult addRole(AdminRole adminRole) {
		adminRole.setCtime(new Date());
		int i = roleService.addRole(adminRole);
		if (i > 0) {
			return BaseResult.build(200, BaseResult.addSuccess);
		} else {
			return BaseResult.build(209, BaseResult.addFail);
		}
	}
	
	
	
	/**
	 * 根据角色id查询角色权限关联表
	 * @Title: selectRolePermissionByRoleId   
	 * @Description: 根据角色id查询角色权限关联表
	 * @param: @param roleId
	 * @param: @return      
	 * @return: BaseResult      
	 * @throws
	 */
	@RequestMapping("/selectRolePermissionByRoleId")
	@ResponseBody
	public BaseResult selectRolePermissionByRoleId(int roleId) {
		List<AdminRolePermission> list = roleService.selectRolePermissionByRoleId(roleId);
		return BaseResult.build(200, "", list);
	}
	
	/**
	 * 根据角色ID 向权限关联表中添加权限id
	 * @Title: addRolePermission   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @param roleId
	 * @param: @param permissionId
	 * @param: @return      
	 * @return: BaseResult      
	 * @throws
	 */
	@RequestMapping("/addRolePermission")
	@ResponseBody
	public BaseResult addRolePermission(int roleId,String permissionId) {
		int i = roleService.addRolePermission(roleId, permissionId);
		if (i > 0) {
			return BaseResult.build(200, BaseResult.addSuccess);
		} else {
			return BaseResult.build(209, BaseResult.addFail);
		}
	}
	
	
}
